package com.dlwx.firefighting.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.dlwx.firefighting.R;
import com.dlwx.firefighting.fragment.fx.FxUserFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FxUserActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.one_tv)
    TextView oneTv;
    @BindView(R.id.two_tv)
    TextView twoTv;
    @BindView(R.id.three_tv)
    TextView threeTv;
    @BindView(R.id.recyclerView)
    LRecyclerView recyclerView;
    @BindView(R.id.fragmentRoot)
    FrameLayout fragmentRoot;
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;

    private FragmentManager fragmentManager;
    private Fragment oneFragment, twoFragment, threeFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fx_user);
        ButterKnife.bind(this);
        fragmentManager = this.getSupportFragmentManager();
        initView();


    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    private void initView() {
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("分销用户");
        creatFragment(0);
        tabLayout.addTab(tabLayout.newTab().setText("一级"));
        tabLayout.addTab(tabLayout.newTab().setText("二级"));
        tabLayout.addTab(tabLayout.newTab().setText("三级"));
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position=tab.getPosition();
                switch (position){
                    case 0:
                        creatFragment(0);
                        break;
                    case 1:
                        creatFragment(1);
                        break;
                    case 2:
                        creatFragment(2);
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @OnClick({R.id.title_left_one_btn, R.id.tv_middle_title, R.id.one_tv, R.id.two_tv, R.id.three_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.tv_middle_title:
                break;
            case R.id.one_tv:
                oneTv.setTextColor(getResources().getColor(R.color.fuzhu_green));
                twoTv.setTextColor(getResources().getColor(R.color.second_textcolor));
                threeTv.setTextColor(getResources().getColor(R.color.second_textcolor));
                creatFragment(0);
                break;
            case R.id.two_tv:
                oneTv.setTextColor(getResources().getColor(R.color.second_textcolor));
                twoTv.setTextColor(getResources().getColor(R.color.fuzhu_green));
                threeTv.setTextColor(getResources().getColor(R.color.second_textcolor));
                creatFragment(1);
                break;
            case R.id.three_tv:
                oneTv.setTextColor(getResources().getColor(R.color.second_textcolor));
                twoTv.setTextColor(getResources().getColor(R.color.second_textcolor));
                threeTv.setTextColor(getResources().getColor(R.color.fuzhu_green));
                creatFragment(2);
                break;
        }
    }

    private void creatFragment(int position) {
        FragmentTransaction transation = fragmentManager.beginTransaction();
        hideFragments(transation);
        Bundle bundle = new Bundle();
        bundle.putInt("loadType", (position + 1));
        switch (position) {
            case 0:
                if (oneFragment == null) {
                    oneFragment = new FxUserFragment();
                    oneFragment.setArguments(bundle);
                    transation.add(R.id.fragmentRoot, oneFragment);
                } else {
                    transation.show(oneFragment);
                }
                break;
            case 1:
                if (twoFragment == null) {
                    twoFragment = new FxUserFragment();
                    twoFragment.setArguments(bundle);
                    transation.add(R.id.fragmentRoot, twoFragment);
                } else {
                    transation.show(twoFragment);
                }
                break;
            case 2:
                if (threeFragment == null) {
                    threeFragment = new FxUserFragment();
                    threeFragment.setArguments(bundle);
                    transation.add(R.id.fragmentRoot, threeFragment);
                } else {
                    transation.show(threeFragment);
                }
                break;
        }
        transation.commitAllowingStateLoss();
    }

    /**
     * 隐藏fragment
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (oneFragment != null) {
            transaction.hide(oneFragment);
        }
        if (twoFragment != null) {
            transaction.hide(twoFragment);
        }
        if (threeFragment != null) {
            transaction.hide(threeFragment);
        }

    }
}
